package com.breakfast.fun.ui.home.comment;

import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;

import com.breakfast.fun.R;
import com.breakfast.fun.base.BaseActivity;
import com.breakfast.fun.event.RefreshEvent;
import com.breakfast.fun.util.T;
import com.breakfast.fun.view.widget.TitleBarView;

import org.greenrobot.eventbus.EventBus;

import butterknife.Bind;

public class CommentActivity extends BaseActivity<CommentPresenter, CommentModel> implements CommentContract.View {

    @Bind(R.id.tbv_comment)
    TitleBarView mTbvComment;
    @Bind(R.id.tv_comment_orderno)
    TextView mTvCommentOrderno;
    @Bind(R.id.tv_comment_ordertime)
    TextView mTvCommentOrdertime;
    @Bind(R.id.sb_comment_product)
    SeekBar mSbCommentProduct;
    @Bind(R.id.tv_comment_product_score)
    TextView mTvCommentProductScore;
    @Bind(R.id.sb_comment_service)
    SeekBar mSbCommentService;
    @Bind(R.id.tv_comment_service_score)
    TextView mTvCommentServiceScore;
    @Bind(R.id.tv_comment_content)
    EditText mTvCommentContent;
    @Bind(R.id.btn_coment_submit)
    Button mBtnComentSubmit;
    @Bind(R.id.tv_comment_text1)
    TextView mTvCommentText1;
    @Bind(R.id.tv_comment_text2)
    TextView mTvCommentText2;

    private String orderId;
    private String orderNo;
    private String orderTime;
    private int orderType = 0;

    private String productScore = "5";
    private String serviceScore = "5";


    @Override
    public int getLayoutId() {
        return R.layout.activity_comment;
    }

    @Override
    public void initView() {

        orderId = getIntent().getStringExtra("orderId");
        orderNo = getIntent().getStringExtra("orderNo");
        orderTime = getIntent().getStringExtra("orderTime");
        orderType = getIntent().getIntExtra("orderType", 0);

        mTbvComment.setTitleNoRight("我要评论");
        mTbvComment.setBack(this);

        mTvCommentOrderno.setText(orderNo);
        mTvCommentOrdertime.setText(orderTime);

        if (orderType == 1) {
            mTvCommentText1.setText("商品质量");
            mTvCommentText2.setText("物流服务");
        }

        mBtnComentSubmit.setOnClickListener(v -> {
            if (TextUtils.isEmpty(mTvCommentContent.getText().toString())) {
                T.show("请输入评价内容");
                return;
            }
            presenter.addComment(orderId, mTvCommentContent.getText().toString(),
                    productScore, serviceScore, orderType);
        });

        mSbCommentProduct.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                productScore = progress + "";
                mTvCommentProductScore.setText(productScore);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        mSbCommentService.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                serviceScore = progress + "";
                mTvCommentServiceScore.setText(serviceScore);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    @Override
    public void showSuccess() {
        T.show("评论成功");
        finish();
        EventBus.getDefault().post(new RefreshEvent("BFOrderFragment"));
        EventBus.getDefault().post(new RefreshEvent("BFOrderDetailActivity"));
    }
}
